package cn.sangyy.org.controller;

import cn.sangyy.auth.annotation.Permission;
import cn.sangyy.basic.utils.AjaxResult;
import cn.sangyy.basic.utils.PageList;
import cn.sangyy.org.domain.Department;
import cn.sangyy.org.query.DepQuery;
import cn.sangyy.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author SangYY
 * @date: 2023/04/23/16:14
 */
@RestController
@RequestMapping("/dep")
@Api(tags = "部门管理")
@Permission(name = "部门管理",desc = "部门管理权限")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    /**
    * @author Sangyy
    * 查所有部门信息
    */
    @ApiOperation(value = "获取所有的部门")
    @GetMapping()
    @Permission(name = "获取所有的部门",desc = "获取所有的部门权限")
    public AjaxResult getAllDep(){
        List<Department> allDep = departmentService.getAllDep();
        return AjaxResult.me().setResultObj(allDep);
    }

    /**
     * @author Sangyy
     * 通过id查部门信息
     */
    @ApiOperation(value = "通过id获取部门信息")
    @GetMapping("/{id}")
    @Permission(name = "通过id获取部门信息",desc = "通过id获取部门信息权限")
    public AjaxResult getDepById(@PathVariable("id") Long id){
        Department dep = departmentService.getDepById(id);
        return AjaxResult.me().setResultObj(dep);
    }

    /**
     * @author Sangyy
     * 通过id删除部门信息
     */
    @ApiOperation(value = "通过id删除部门信息")
    @DeleteMapping("/{id}")
    @Permission(name = "通过id删除部门信息",desc = "通过id删除部门信息权限")
    public AjaxResult deleteDepById(@PathVariable("id") Long id){
        boolean flag = departmentService.deleteById(id);
        if(flag){
            return AjaxResult.me();
        }
        else{
            return AjaxResult.me().setCode(400).setMessage("删除失败！");
        }
    }

    /**
    * @author Sangyy
    * 批量删除部门
    */
    @ApiOperation(value = "批量删除部门")
    @Permission(name = "批量删除部门",desc = "批量删除部门权限")
    @DeleteMapping("/batchDelete")
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        try {
            departmentService.batchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setCode(400).setMessage(e.getMessage());
        }
    }

    /**
     * @author Sangyy
     * 新增or修改dep
     */
    @ApiOperation(value = "新增或者修改的部门信息")
    @PutMapping()
    @Permission(name = "新增或者修改的部门信息",desc = "新增或者修改的部门信息权限")
    public AjaxResult addOrUpdateDep(@RequestBody Department department){
        Long id = department.getId();
        boolean flag;
        if(id == null){
            flag = departmentService.insert(department);
        }else {
            flag = departmentService.update(department);
        }
        if(flag){
            return AjaxResult.me();
        }
        else{
            return AjaxResult.me().setCode(400).setMessage("新增失败！");
        }
    }

    /**
    * @author Sangyy
    * 分页加条件查询部门信息
    */
    @ApiOperation(value = "分页加条件查询部门")
    @Permission(name = "分页加条件查询部门信息",desc = "分页加条件查询部门信息权限")
    @PostMapping()
    public AjaxResult getDepPageByKeyword(@RequestBody DepQuery depQuery){
        try {
            PageList<Department> pageList =  departmentService.getDepPageByKeyword(depQuery);
            return AjaxResult.me().setResultObj(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setCode(400).setMessage(e.getMessage());
        }
    }

    /**
    * @author Sangyy
    * 查部门树 这里只查到三级
    */
    @ApiOperation(value = "查部门树")
    @GetMapping("/depTree")
    @Permission(name = "查部门树",desc = "查部门树权限")
    public AjaxResult depTree(){
        try {
            List<Department> list = departmentService.depTree();
            return AjaxResult.me().setResultObj(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setCode(201).setMessage("操作失败"+e.getMessage());
        }
    }
}
